from django.shortcuts import render
from rest_framework import viewsets
from rest_framework import mixins
from django_filters.rest_framework import DjangoFilterBackend
from .models import MovieDetail
from .filters import MovieDetailFilter
from .serializers import FilmRowPieceRetrieveSerializer,MovieListSerializer, MovieDetailSerializer, MovieDetailUpdateSerializer,FilmRowPieceSerializer
# Create your views here.
from rest_framework import filters
from django.views.decorators.cache import cache_page


class MovieViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, mixins.UpdateModelMixin, viewsets.GenericViewSet):
    """
    获取电影信息接口
    """

    queryset = MovieDetail.objects.all()
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    search_fields = ('name',)
    ordering_fields = ('id',)
    filter_class = MovieDetailFilter

    def get_serializer_class(self):
        if self.action == 'list':
            return MovieListSerializer
        if self.action == 'retrieve':
            return MovieDetailSerializer
        if self.action in ('update', 'partial_update'):
            return MovieDetailUpdateSerializer


class FilmRowPieceViewSet(viewsets.ReadOnlyModelViewSet):

    serializer_class = FilmRowPieceSerializer
    queryset = serializer_class.Meta.model.objects.all()

    def get_serializer_class(self):
        if self.action == 'retrieve':
            return FilmRowPieceRetrieveSerializer

        return FilmRowPieceSerializer



